﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace GUI
{
    public partial class uc_ChucVu : DevExpress.XtraEditors.XtraUserControl
    {
        #region Khai báo biến

        ChucVu_BLL cv_bll = new ChucVu_BLL();
        TextMessageBox box = new TextMessageBox();
        DataTable dt = new DataTable();

        #endregion

        #region LoafForm
        public uc_ChucVu()
        {
            InitializeComponent();
        }
        
        private void uc_ChucVu_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion

        #region Các chức năng
        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChucVu frm = new frmChucVu();
            if (frm.ShowDialog() == DialogResult.OK)
                LoadData();
        }

        private void btnSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.RowCount > 0)
            {
                try
                {
                    frmChucVu frm = new frmChucVu();
                    frm.maChucVu = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaChucVu).ToString();
                    if (frm.ShowDialog() == DialogResult.OK)
                        LoadData();
                }
                catch (Exception)
                {
                    XtraMessageBox.Show(box.focuesFillter, box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
                XtraMessageBox.Show(box.NotData,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }

        private void txtXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grv_Data.RowCount > 0)
            {
                ChucVu_Public cv = new ChucVu_Public();
                try
                {
                    string maChucVu = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colMaChucVu).ToString();
                    cv.MaChucVu = maChucVu;
                    string tenChucVu = grv_Data.GetRowCellValue(grv_Data.FocusedRowHandle, colTenChucVu).ToString();
                    dt = cv_bll.TaoBang(box.sqlCheckExist("CTDG_GiaoVien", "MaChucVu", maChucVu));
                    if (dt.Rows.Count > 0)
                    {
                        XtraMessageBox.Show(box.NotDelete(maChucVu), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        if (XtraMessageBox.Show(box.Delete_Confirm(maChucVu, tenChucVu), box.Caption_Confirm, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                        {
                            try
                            {
                                cv_bll.Delete(cv);
                                LoadData();
                            }
                            catch (Exception)
                            {
                                XtraMessageBox.Show(box.DeleteFail, box.Caption_Fail, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    XtraMessageBox.Show(box.focuesFillter, box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
                XtraMessageBox.Show(box.NotData,box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }
        #endregion

        #region Hàm viết thêm
        private void LoadData()
        {
            grd_Data.DataSource = cv_bll.LayDuLieu();
        }

        #endregion
    }
}
